 package com.example.testcomposeproject.pages.sideEffect.testProduceState

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Warning
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Recomposer
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.testcomposeproject.MyLogger
import com.example.testcomposeproject.PreViewData
import com.example.testcomposeproject.R
import com.example.testcomposeproject.pages.copy.CopyViewModel
import com.example.testcomposeproject.ui.theme.AppTheme
import com.ioidea.base.widget.Toolbar

 /**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc: 这个思路可以包装高德地图的MapView
 *
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestProduceStateUI(modifier: Modifier = Modifier) {
    AppTheme {
        TestProduceStateUI(modifier = modifier)

    }
}




/**
 * viewModel 用这个方法
 */
@Composable
fun TestProduceStateScreen(
    modifier: Modifier = Modifier,
    viewModel: CopyViewModel = hiltViewModel()
) {
    TestProduceStateUI(modifier)
}

/**
 * 真正的UI元素
 */
@Composable
fun TestProduceStateUI(modifier: Modifier = Modifier) {

    val imagesList = listOf<String>(
        "https://pic.rmb.bdstatic.com/bjh/gallery/0e9fc1b0b08b47c68e82692277d66ddf6020.jpeg",
        "https://img1.baidu.com/it/u=3753266459,121061334&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1371",
        "https://pic.rmb.bdstatic.com/bjh/video/b845731d70db30540a37d83cd9442433.jpeg?for=bg",
    )

    var index by remember {
        mutableIntStateOf(0)
    }

    val imageRepository = ImageRepository(LocalContext.current)
    //result变化自动刷新ui
    val result = LoadNetworkImage(imagesList[index], imageRepository)

//    Column(
//        modifier = Modifier.fillMaxSize(),
//
//    )  {
     Toolbar(title = stringResource(R.string.title_produce_state))
//    }
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,
        verticalArrangement = androidx.compose.foundation.layout.Arrangement.Center
    ) {
        Button(onClick = {
            index %= imagesList.size
            if (++index == imagesList.size) {
                index = 0
            }


        }) {
            Text(text = "请选择第${index + 1}张图片")
        }

        when (result.value) {
            is Result.Loading -> {
                CircularProgressIndicator()
            }

            is Result.Error -> {
                Image(
                    imageVector = Icons.Rounded.Warning,
                    contentDescription = "加载失败",modifier=Modifier.size(200.dp)
                )
            }

            is Result.Success -> {

                Image(
                    bitmap = (result.value as Result.Success).image.imageBitmap,
                    contentDescription = "加载成功"
                )
            }

        }

    }
}

 @Composable
 fun LoadNetworkImage(url:String,
                      imageRepository: ImageRepository): State<Result<Image>> {

     return produceState(initialValue = Result.Loading as Result<Image>,url,imageRepository) {

         MyLogger.hLog().e("开始加载图片-->看下线程")
         val image= imageRepository.load(url)

         value = if (image ==null){
             Result.Error
         }else{
             Result.Success(image)
         }
     }
 }


